﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Users.aspx.cs" Inherits="Modules_HeThong_PhanQuyen_NguoiDung" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <script language="javascript" type="text/javascript">
        var commandHandler = function (cmd, record) {
            switch (cmd) {
                case "edit":
                    Ext.net.DirectMethods.LoadEditForm(record.data.UserName, {
                        success: function (result) {
                            var myMask = new Ext.LoadMask(gridViewUser.getBody(), { msg: "Tải form..." });
                            myMask.hide();
                        },
                        before: function () {
                            var myMask = new Ext.LoadMask(gridViewUser.getBody(), { msg: "Tải form..." });
                            myMask.show();
                        }
                    });
                    break;
                case "delete":
                    Ext.Msg.confirm('Xác nhận', '<span style="color:red;">Xóa người dùng:</span><b> ' + record.data.UserName + '</b>', function (btn) {
                        if (btn == "yes") {
                            Ext.net.DirectMethods.DeleteUser(record.data.UserName, {
                                success: function (result) {
                                    var myMask = new Ext.LoadMask(gridViewUser.getBody(), { msg: "Tải form..." });
                                    myMask.hide();
                                    storeUser.reload();

                                },
                                before: function () {
                                    var myMask = new Ext.LoadMask(gridViewUser.getBody(), { msg: "Xóa người dùng..." });
                                    myMask.show();
                                }
                            });
                        }
                    });
                    break;
            }
        }

        var addUser = function () {
            Ext.net.DirectMethods.UpdateUser({
                success: function (result) {
                    var myMask = new Ext.LoadMask(wdAddUser.getBody(), { msg: "Cập nhật dữ liệu..." });
                    myMask.hide();
                    storeUser.reload();

                },
                before: function () {
                    var myMask = new Ext.LoadMask(wdAddUser.getBody(), { msg: "Cập nhật dữ liệu..." });
                    myMask.show();
                }
            });
        }

        var saveUser = function () {
            Ext.net.DirectMethods.UpdateUser({
                success: function (result) {
                    var myMask = new Ext.LoadMask(wdAddUser.getBody(), { msg: "Cập nhật dữ liệu..." });
                    myMask.hide();
                    storeUser.reload();
                    //                    alert(result);
                },
                before: function () {
                    var myMask = new Ext.LoadMask(wdAddUser.getBody(), { msg: "Cập nhật dữ liệu..." });
                    myMask.show();
                }
            });
        }

        var applyFilter = function () {
            storeUser.reload();

        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <ext:ResourceManager ID="ResourceManager1" runat="server">
    </ext:ResourceManager>
    <ext:Viewport ID="ViewPort2" runat="server" Layout="Fit">
        <Items>
            <ext:GridPanel ID="gridViewUser" runat="server" StripeRows="true" Layout="Fit" Header="false"
                Region="Center" Frame="false" Border="false" TrackMouseOver="true" Title="Toàn bộ Văn bản nội bộ"
                AutoExpandColumn="Email">
                <TopBar>
                    <ext:Toolbar>
                        <Items>
                            <ext:Button runat="server" ID="btnAddUser" Icon="Add" Text="Thêm người dùng">
                                <Listeners>
                                    <Click Handler="txtEmail.setValue('');txtUserName.setValue('');txtPassword.setValue(''); wdAddUser.show();" />
                                </Listeners>
                            </ext:Button>
                        </Items>
                    </ext:Toolbar>
                </TopBar>
                <Store>
                    <ext:Store ID="storeUser" runat="server">
                        <Reader>
                            <ext:JsonReader Root="users" TotalProperty="total" IDProperty="UserName">
                                <Fields>
                                    <ext:RecordField Name="UserName" Type="String" />
                                    <ext:RecordField Name="Email" Type="String" />
                                    <ext:RecordField Name="IsLockedOut" Type="String" />
                                </Fields>
                            </ext:JsonReader>
                        </Reader>
                        <Proxy>
                            <ext:HttpProxy Method="GET" Json="true" Url="UserHandler.ashx" />
                        </Proxy>
                    </ext:Store>
                </Store>
                <ColumnModel ID="ColumnModel1" runat="server">
                    <Columns>
                        <ext:RowNumbererColumn runat="server" Width="30" Header="STT" />
                        <ext:CommandColumn runat="server" Width="30" Header="Sửa">
                            <Commands>
                                <ext:GridCommand Icon="NoteEdit" CommandName="edit">
                                    <ToolTip Text="Sửa" />
                                </ext:GridCommand>
                            </Commands>
                        </ext:CommandColumn>
                        <ext:CommandColumn Header="Xóa" Width="30">
                            <Commands>
                                <ext:GridCommand Icon="Delete" CommandName="delete">
                                    <ToolTip Text="Xóa" />
                                </ext:GridCommand>
                            </Commands>
                        </ext:CommandColumn>
                        <ext:Column runat="server" Header="Tên đăng nhập" Sortable="true" Width="180" DataIndex="UserName">
                        </ext:Column>
                        <ext:Column runat="server" Header="Email" Sortable="true" DataIndex="Email">
                        </ext:Column>
                        <%--         <ext:Column runat="server" Text="IsLockedOut" Sortable="true" DataIndex="IsLockedOut">
                        </ext:Column>--%>
                    </Columns>
                </ColumnModel>
                <Listeners>
                    <Command Handler="commandHandler(command, record);" />
                </Listeners>
                <SelectionModel>
                    <ext:RowSelectionModel ID="RowSelectionModel1" runat="server" Mode="Multi" />
                </SelectionModel>
                <View>
                    <ext:GridView ID="GridView1" runat="server" StripeRows="true">
                    </ext:GridView>
                </View>
                <BottomBar>
                    <ext:PagingToolbar ID="PagingToolbar1" runat="server">
                    </ext:PagingToolbar>
                </BottomBar>
            </ext:GridPanel>
        </Items>
    </ext:Viewport>
    <%--Tạo người dùng--%>
    <ext:Window ID="wdAddUser" runat="server" Collapsible="false" Icon="UserAdd" Title="Thêm mới người dùng"
        Hidden="true" Modal="false" Height="160" BodyStyle="background-color:#fff;padding:10px;"
        Width="360" >
        <Buttons>
            <ext:Button Icon="Disk" Text="Lưu">
                <DirectEvents>
                    <Click OnEvent="AddUser" Before=" var valid= #{FormPanel1}.getForm().isValid(); if (valid) {Ext.Msg.wait('Đang cập nhật dữ liêu...', 'Saving')} return valid; ">
                    </Click>
                </DirectEvents>
            </ext:Button>
            <ext:Button Icon="ArrowUndo" Text="Hủy bỏ">
                <Listeners>
                    <Click Handler="wdAddUser.hide();" />
                </Listeners>
            </ext:Button>
        </Buttons>
        <Items>
            <ext:FormPanel ID="FormPanel1" runat="server" Border="false" AutoDoLayout="true"
                BodyPadding="10">
                <Items>
                    <ext:TextField runat="server" ID="txtUserName" Width="200" FieldLabel="Tên đăng nhập"
                        AllowBlank="false">
                    </ext:TextField>
                    <ext:TextField runat="server" ID="txtEmail" Width="200" FieldLabel="Email" AllowBlank="false"
                        Vtype="email">
                    </ext:TextField>
                    <ext:TextField FieldLabel="Mật khẩu" runat="server" ID="txtPassword" Width="200"
                        AllowBlank="false">
                    </ext:TextField>
                </Items>
            </ext:FormPanel>
        </Items>
    </ext:Window>
    <%-- Chỉnh sửa người dùng--%>
    <ext:Window ID="wdEditUser" runat="server" Collapsible="false" Icon="UserEdit" Title="Chỉnh sửa người dùng"
        BodyStyle="background-color:#fff;padding:10px;" Modal="false" Hidden="true" Height="280"
        Width="460">
        <Buttons>
            <ext:Button Icon="Disk" Text="Lưu">
                <DirectEvents>
                    <Click OnEvent="UpdateUser" Before=" var valid= #{StatusForm}.getForm().isValid(); if (valid) {Ext.Msg.wait('Đang cập nhật dữ liêu...', 'Saving')} return valid; ">
                    </Click>
                </DirectEvents>
            </ext:Button>
            <ext:Button Icon="ArrowUndo" Text="Hủy bỏ">
                <Listeners>
                    <Click Handler="wdEditUser.hide();" />
                </Listeners>
            </ext:Button>
        </Buttons>
        <Items>
            <ext:FormPanel ID="StatusForm" runat="server" Border="false" AutoDoLayout="true"
                BodyBorder="false" BodyPadding="10">
                <Items>
                    <ext:Hidden ID="hdfCurrentUsername" runat="server">
                    </ext:Hidden>
                    <ext:FieldSet ID="FieldSet2" runat="server" Title="Thông tin chung" Layout="AnchorLayout"
                        Padding="5" Collapsed="false" Collapsible="true" DefaultAnchor="100%">
                        <Defaults>
                            <ext:Parameter Name="LabelStyle" Value="padding-left:4px;" />
                        </Defaults>
                        <Items>
                            <ext:TextField runat="server" ID="txtEditUsername" Enabled="false" Width="200" ReadOnly="true"
                                FieldLabel="Tên đăng nhập">
                            </ext:TextField>
                            <ext:TextField runat="server" ID="txtEditEmail" Width="200" Enabled="false" FieldLabel="Email"
                                ReadOnly="true">
                            </ext:TextField>
                        </Items>
                    </ext:FieldSet>
                    <ext:FieldSet ID="FieldSet1" runat="server" Title="Thiết lập mật khẩu" Layout="AnchorLayout"
                        Padding="5" Collapsed="true" Collapsible="true" DefaultAnchor="100%">
                        <Defaults>
                            <ext:Parameter Name="LabelStyle" Value="padding-left:4px;" />
                        </Defaults>
                        <Items>
                            <ext:TextField runat="server" ID="txtResetPassword1" Width="200" FieldLabel="Mật khẩu mới"
                                Note="Nhập vào mật khẩu mới để thay đổi mật khẩu">
                            </ext:TextField>
                        </Items>
                    </ext:FieldSet>
                </Items>
            </ext:FormPanel>
        </Items>
    </ext:Window>
    </form>
</body>
</html>
